package com.jikangjun.disk.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class CaptchaUtil {
    // 验证码字符集
    private static final String CODE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    // 验证码长度
    private static final int CODE_LENGTH = 4;
    // 图片宽度
    private static final int IMAGE_WIDTH = 120;
    // 图片高度
    private static final int IMAGE_HEIGHT = 40;
    // 字体大小
    private static final int FONT_SIZE = 20;

    private static final Random random = new Random();

    /**
     * 生成验证码图片
     * @param outputStream 输出流，用于将图片写入响应
     * @return 生成的验证码字符串
     * @throws IOException 输出异常
     */
    public static String generateCaptcha(OutputStream outputStream) throws IOException {
        // 创建图片缓冲区
        BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        // 获取绘图对象
        Graphics graphics = image.getGraphics();

        // 设置背景色
        graphics.setColor(getRandomColor(200, 250));
        graphics.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);

        // 设置字体
        graphics.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, FONT_SIZE));

        // 绘制干扰线
        for (int i = 0; i < 5; i++) {
            graphics.setColor(getRandomColor(150, 200));
            int x1 = random.nextInt(IMAGE_WIDTH);
            int y1 = random.nextInt(IMAGE_HEIGHT);
            int x2 = random.nextInt(IMAGE_WIDTH);
            int y2 = random.nextInt(IMAGE_HEIGHT);
            graphics.drawLine(x1, y1, x2, y2);
        }

        // 绘制噪点
        for (int i = 0; i < 30; i++) {
            graphics.setColor(getRandomColor(100, 200));
            int x = random.nextInt(IMAGE_WIDTH);
            int y = random.nextInt(IMAGE_HEIGHT);
            graphics.fillOval(x, y, 2, 2);
        }

        // 生成验证码字符并绘制
        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            // 随机获取字符
            char codeChar = CODE_CHARACTERS.charAt(random.nextInt(CODE_CHARACTERS.length()));
            codeBuilder.append(codeChar);

            // 设置随机颜色
            graphics.setColor(getRandomColor(50, 150));

            // 随机旋转角度 (-30到30度)
            Graphics2D g2d = (Graphics2D) graphics;
            int angle = random.nextInt(60) - 30;
            g2d.rotate(Math.toRadians(angle), 20 + i * 25, 25);

            // 绘制字符
            g2d.drawString(String.valueOf(codeChar), 20 + i * 25, 25);

            // 恢复旋转
            g2d.rotate(-Math.toRadians(angle), 20 + i * 25, 25);
        }

        // 释放资源
        graphics.dispose();

        // 将图片写入输出流
        ImageIO.write(image, "JPEG", outputStream);

        return codeBuilder.toString();
    }

    /**
     * 生成随机颜色
     * @param min 最小色值
     * @param max 最大色值
     * @return 随机颜色
     */
    private static Color getRandomColor(int min, int max) {
        if (min > 255) min = 255;
        if (max > 255) max = 255;

        int red = min + random.nextInt(max - min);
        int green = min + random.nextInt(max - min);
        int blue = min + random.nextInt(max - min);

        return new Color(red, green, blue);
    }
}
